const path = require('path');
const express = require('express');
const autoprefixer = require('autoprefixer');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
const DashboardPlugin = require('webpack-dashboard/plugin');

const PATHS = {
    src: path.resolve(__dirname, 'src'),
    scss: path.resolve(__dirname, 'src/scss'),
    res: path.resolve(__dirname, 'res'),
    output: path.resolve(__dirname, 'www')
};

const output = {
    path: PATHS.output,
    filename: '[name].js'
};

const config = {
    entry: {
        polyfills: path.resolve(PATHS.src, 'polyfills.ts'),
        vendor: path.resolve(PATHS.src, 'vendor.ts'),
        main: path.resolve(PATHS.src, 'main.ts'),
        style: path.resolve(PATHS.scss, 'vendor.scss')
    },
    output,
    plugins: [
        new ExtractTextPlugin('[name][hash].css'),
        new ForkCheckerPlugin(),
        new ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/),
        new LoaderOptionsPlugin({
            minimize: true,
            debug: false,
            options: {
                sassLoader: {
                    includePaths: [
                        PATHS.scss,
                        path.resolve(__dirname, 'node_modules/ionicons/dist/scss')
                    ]
                },
                postcss: [autoprefixer({ browsers: ['last 3 versions'] })],
                context: '/',
                output
            }
        }),
        new CommonsChunkPlugin({ name: ['vendor', 'polyfills'] }),
        new HtmlWebpackPlugin({ template: path.resolve(PATHS.src, 'index.html'), chunksSortMode: 'dependency' }),
        new DashboardPlugin()
    ],
    resolve: {
        extensions: ['.ts', '.js', '.json'],
        modules: [PATHS.src, 'node_modules']
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader'],
            },
            {
                test: /\.html$/,
                loader: 'raw'
            },
            { 
                test: /\.css$/, 
                loaders: ['to-string-loader', 'css-loader','postcss-loader'] 
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('css?-autoprefixer!postcss!sass'),
                include: PATHS.scss
            },
            {
                test: /\.json$/,
                loader: 'json'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?v=[\w\.-]*)?$/,
                loader: 'file?name=fonts/[name].[hash].[ext]?'
            }
        ]
    },
    node: {
        process: true,
        module: false,
        clearImmediate: false,
        setImmediate: false
    },
    devServer: {
        quiet: true,
        stats: 'minimal',
        historyApiFallback: true,
        outputPath: PATHS.output,
        watchOptions: { aggregateTimeout: 300, poll: 1000 },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
            "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
        },
        setup: function(app) {
            app.use('/', express.static(PATHS.res));
            app.use(express.static(path.join(__dirname, 'platforms/browser/platform_www')));
            app.get('/config.xml', (req, res) => res.sendFile(path.join(__dirname, '/config.xml')));
        }
    }
};

module.exports = config;